#include "ComboBoxWidget.h"

#include "TextWidget.h"
#include "Mouse/MouseWidget.h"

#include "Framework/Application.h"
#include "Widgets/WindowManager.h"
#include "Widgets/ListWidget.h"
#include "Widgets/RootWidget.h"
#include "Widgets/WidgetTree.h"

RComboBoxWidget::RComboBoxWidget()
{
    Selected= NewWidget<RTextWidget>(this);
    Selected->Anchor.Fill(this);
    Selected->Text=CString(U"Nothing Selected");
    Selected->VerticalAlignment=ETextVerticalAlignment::Center;

    auto Mouse =NewWidget<RMouseWidget>(this);
    Mouse->Anchor.Fill(this);
    Mouse->OnClicked.Bind(this,&RComboBoxWidget::OnClick);

}

bool RComboBoxWidget::IsPopWindowOpen() const 
{
    return PopWindow.IsValid();
}


bool RComboBoxWidget::OnClick(const SMouseButtonEvent&)
{
    if(!IsPopWindowOpen())
    {
        ShowPopWindow();
        return true;
    }
    return false;
}

void RComboBoxWidget::SetCurrentText(const CString &InText)
{
    Selected->Text=InText;
}

CString RComboBoxWidget::GetCurrentText() const
{
    return Selected->Text.Get();
}

void RComboBoxWidget::SetOptions(const TVector<CString> &InOptions)
{
    ListItems=InOptions;
    if(ListItems.Empty())
    {
        SetCurrentText(U"Nothing Selected");
        return ;
    }
    SetCurrentText(ListItems[0]);

}

void RComboBoxWidget::ShowPopWindow()
{
    //TVector<CString> ListTextItems=ListItems.Get();

    if(!PopWindow)
    {
        PopWindow=CreatePopWindow(this->GetWindow());
        //Fill pop window content here

        auto List=NewWidget<RListWidget>(PopWindow->GetRootWidget());
        List->Anchor.Fill(PopWindow->GetRootWidget());
        List->FillColor=SColor::FromHexRGB(0x1C1C1C);

        auto ItemContainer=List->GetContentContainer();
        for(uint32_t ItemIndex=0; ItemIndex<ListItems.Num(); ItemIndex++ )
        //for(auto& ListTextItem:ListItems)
        {   
            CString ListTextItem=ListItems[ItemIndex];
            auto ItemWidget=NewWidget<RTextWidget>(ItemContainer);
            ItemWidget->Text=ListTextItem;
            ItemWidget->Height=20.f;
            ItemWidget->Anchor.LeftReference=ItemContainer;
            ItemWidget->Anchor.RightReference=ItemContainer;

            auto ItemMouse=NewWidget<RMouseWidget>(ItemWidget);
            ItemMouse->Anchor.Fill(ItemWidget);
            ItemMouse->OnClicked.BindLambda([this,ListTextItem,ItemIndex ](const SMouseButtonEvent&) ->bool {
                Selected->Text=ListTextItem;
                OnSelected.ExecuteIfBound(ListTextItem,ItemIndex);
                this->HidePopWindow();
                return true;
            });
        }
        ItemContainer->ArrangeChildren();

        PopWindow->SetWindowSize(SIntPoint(Selected->GetPresentSize().X,ItemContainer->GetContentPresentLength()));

        PopWindow->Show();
    }


    PopWindow->SetWindowPosition(SIntPoint(Selected->GetPresentAbsolutePosition()+SVec2(0,Selected->GetPresentSize().Y))+Selected->GetWidgetTree()->GetWindow()->GetWindowPosition());

}

void RComboBoxWidget::HidePopWindow()
{
    if(PopWindow)
    {
        if(!PopWindow->IsClosed())
        {
            this->GetWindow()->CloseAttachedPopWindow();
        }
        PopWindow=nullptr;
    } 

}
